<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-08
 * Time: 15:09
 */

namespace app\service;


use app\model\LoginfoModel;
use think\Exception;

/**
 * 日志
 * Class LogService
 * @package app\service
 */
class LogService extends BaseService
{
    public $logModel;
    public function __construct()
    {
        $this->logModel=new LoginfoModel();//M('loginfo');
    }

    /**
     * @param $type
     * @param $info
     * @param null $data
     * @param null $remark
     */
    public function writeLog($type,$info,$data=null,$remark=null){
        try{
            $add['type']=$type;
            $add['info']=$info;
            if(is_array($data)){
                $data=json_encode($data);
            }
            $add['data']=$data;
            $add['create_time']=date('Y-m-d H:i:s');
            $add['api']=$_SERVER['REQUEST_URI'];
            $add['remark']=json_encode($remark);
            return $this->logModel->save($add);
        }catch (Exception $ex){
            $addEx['type']='Exception';
            $addEx['info']=$ex->getMessage();
            $addEx['data']=json_encode($add);
            $addEx['create_time']=date('Y-m-d H:i:s');
            $add['api']=$_SERVER['REQUEST_URI'];
            $this->logModel->save($addEx);
        }
    }

    public function writeLogs($data = array()){
    try{
//        $add['type']=$type;
//        $add['info']=$info;
//        if(is_array($data)){
//            $data=json_encode($data);
//        }
//        $add['data']=$data;
//        $add['create_time']=date('Y-m-d H:i:s');
//        $add['api']=$_SERVER['REQUEST_URI'];
//        $add['remark']=json_encode($remark);
        if($data)
        return $this->logModel->insertAll($data);
    }catch (Exception $ex){
        $addEx['type']='Exception';
        $addEx['info']=$ex->getMessage();
        $addEx['data']=json_encode($add);
        $addEx['create_time']=date('Y-m-d H:i:s');
        $add['api']=$_SERVER['REQUEST_URI'];
        $this->logModel->save($addEx);
    }
}


    /**
     * @param null $type
     * @param null $id
     * @return bool|false|\PDOStatement|string|\think\Collection
     */
    public function queryLogInfo($type=null,$id=null){
        try{
            $where=array();
            if(!empty($type)){
                $where['type']=$type;
            }
            if(!empty($id)){
                $where['id']=$id;
            }
            return $this->logModel->where($where)->select();
        }catch (Exception $ex){
            return false;
        }
    }

    /**
     * 删除日志信息
     * @param null $type
     * @param null $id
     * @return mixed
     */
    public function delLog($type=null,$id=null){
        $where=array();
        if(!empty($type)){
            $where['type']=$type;
        }
        if(!empty($id)){
            $where['id']=$id;
        }
        if(empty($where)){
            return false;
        }
        return $this->logModel->where($where)->delete();
    }

}